<?php

$myts =& MyTextSanitizer::getInstance() ;
$db =& Database::getInstance() ;
require_once dirname(dirname(__FILE__)).'/include/common_functions.php' ;

// THIS PAGE CAN BE CALLED ONLY FROM D3DOWNLOADS
if( $xoopsModule->getVar('dirname') != $mydirname ) die( 'this page can be called only from '.$mydirname ) ;

// PERMISSION ERROR
$module_handler =& xoops_gethandler( 'module' ) ;
$module =& $module_handler->getByDirname( $mydirname ) ;
$moduleperm_handler =& xoops_gethandler( 'groupperm' ) ;
$mid = $module->getVar('mid') ;
if( ! is_object( @$xoopsUser ) || ! $moduleperm_handler->checkRight( 'module_admin' , $mid , $xoopsUser->getGroups() ) ) {
	die( 'Only administrator can use this feature.' ) ;
}

// GET ID FROM $_GET
$id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : "";

$mod_url = XOOPS_URL.'/modules/'.$mydirname ;
// GET HISTORY DATA
$history4assign = array() ;
$broken_link = 0 ;
$result = $db->query("SELECT h.id, h.lid, h.cid, h.title, h.url, h.filename, h.ext, h.description, h.date, c.title FROM ".$db->prefix( $mydirname."_downloads_history" )." h LEFT JOIN ".$db->prefix($mydirname."_cat")." c ON h.cid=c.cid WHERE id='".$id."'");
while( list( $history_id, $history_lid, $history_cid, $history_title, $history_url, $history_filename, $history_ext, $history_description, $history_date, $ctitle ) = $db->fetchRow( $result ) ) {
	$id = intval( $history_id );
	$lid = intval( $history_lid );
	$history_url4assign = $myts->makeTboxData4Show( $history_url );
	$filename = $myts->makeTboxData4Show( $history_filename ) ;
	$ext = $myts->makeTboxData4Show( $history_ext ) ;
	if ( ! preg_match("/^http:\/\//", $history_url4assign) ) {
		if( ! file_exists( $history_url4assign ) ){
			$filelink = $history_url4assign.'<br />( <font color="#FF0000"><b>broken file !!</b></font> )';
		} else {
			if ( ! empty( $filename ) ){
				$filelink =  '<a href="'.$mod_url.'/index.php?page=visit_url&url='.$history_url4assign.'&filename='.$filename.'&ext='.$ext.'" id="access_url">'.$history_url4assign.'</a>' ;
			} else {
				$filelink =  '<a href="'.$mod_url.'/index.php?page=visit_url&url='.$history_url4assign.'" id="access_url">'.$history_url4assign.'</a>' ;
			}
		}
	} else {
		$filelink =  '<a href="'.$history_url4assign.'" id="access_url" rel="nofollow">'.$history_url4assign.'</a>' ;
	}
	$history4assign = array(
		'id' => $id ,
		'lid' => $lid ,
		'cid' => intval( $history_cid ) ,
		'title' => $myts->makeTboxData4Show( $history_title ) ,
		'url' => $history_url4assign ,
		'filelink' => $filelink ,
		'filename' => $myts->makeTboxData4Show( $history_filename ) ,
		'ext' => $myts->makeTboxData4Show( $history_ext ) ,
		'description' => $myts->displayTarea( $history_description, 0, 1, 1, 1, 1 ) ,
		'date' => formatTimestamp(intval( $history_date )) ,
		'ctitle' => $myts->makeTboxData4Show( $ctitle ) ,
		'broken_link' => $broken_link ,
	) ;
}

// GET HISTORY lIST
$historylist = array() ;
$hrs = $db->query("SELECT id, lid, cid, title, url, description, date FROM ".$db->prefix( $mydirname."_downloads_history" )." WHERE lid='".$lid."' AND id NOT IN ('".$id."') ORDER BY id DESC");
while( list( $h_id, $h_lid, $h_cid, $h_title, $h_url, $h_description, $h_date ) = $db->fetchRow( $hrs ) ) {
	$historylist[] = array(
		'history_id' => intval( $h_id ) ,
		'history_lid' => intval( $h_lid ) ,
		'history_cid' => intval( $h_cid ) ,
		'history_title' => $myts->makeTboxData4Show( $h_title ) ,
		'history_url' => $myts->makeTboxData4Show( $h_url ) ,
		'history_description' => $myts->makeTboxData4Show( $h_description ) ,
		'history_date' => formatTimestamp(intval( $h_date )) ,
	) ;
}

// GET DOWNLOADDATA
$download4assign = array();
$broken_link = 0 ;
$nrs = $db->query("SELECT d.cid, d.title, d.url, d.filename, d.ext, d.homepage, d.version, d.size, d.platform, d.logourl, d.description, d.html, d.smiley, d.br, d.xcode, d.submitter, d.date, c.title FROM ".$db->prefix( $mydirname."_downloads" )." d LEFT JOIN ".$db->prefix( $mydirname."_cat" )." c ON d.cid=c.cid WHERE d.lid='".$lid."'");
while( list( $cid, $title, $url, $filename, $ext, $homepage, $version, $size, $platform, $logourl, $description, $html, $smiley, $br, $xcode, $submitter, $date, $category ) = $db->fetchRow( $nrs ) ) {
	$cid4assign = intval( $cid ) ;
	$url4assign = $myts->makeTboxData4Show( $url ) ;
	$title4assign = $myts->makeTboxData4Show( $title ) ;
	$filename = $myts->makeTboxData4Show( $filename ) ;
	$ext = $myts->makeTboxData4Show( $ext ) ;
	if ( ! preg_match("/^http:\/\//", $url4assign) ) {
		if( ! file_exists( $url4assign ) ){
			$broken_link = 1 ;
			$filelink = "";
			$logourl4assign = "";
			$shots_link = "";
		} else {
			if ( ! empty( $filename ) ){
				$filelink =  '<a href="'.$mod_url.'/index.php?page=visit_url&url='.$url4assign.'&filename='.$filename.'&ext='.$ext.'">' ;
			} else {
				$filelink =  '<a href="'.$mod_url.'/index.php?page=visit_url&url='.$url4assign.'">' ;
			}
		}
	} else {
		$filelink =  '<a href="'.$mod_url.'/index.php?page=visit_url&url='.$url4assign.'" rel="nofollow">' ;
	}
	if( ! empty( $filelink ) ){
		$logourl4assign = ! empty( $logourl ) ? $myts->makeTboxData4Show( $logourl ) :"";
		$shots_link = d3download_shots_link( $mydirname, $cid4assign, $url4assign, $filelink, $logourl4assign ) ;
	}
	$submitter =  intval( $submitter ) ;
	if ( strstr ( $description , '[pagebreak]' ) ){
		$body = str_replace( '[pagebreak]','', $description  ) ;
	} else {
		$body = $description ;
	}
	$body4assign = $myts->displayTarea( $body, intval( $html ), intval( $smiley ), intval( $xcode ), 1, intval( $br ) ) ;
	$download4assign = array(
		'cid' => $cid4assign ,
		'category' => $category ,
		'title' => $title4assign ,
		'url' => $url4assign ,
		'filelink' => $filelink ,
		'homepage' => $myts->makeTboxData4Show( $homepage ) ,
		'version' => $myts->makeTboxData4Show( $version ) ,
		'size' =>  intval( $size ) ,
		'platform' => $myts->makeTboxData4Show( $platform ) ,
		'logourl' => $logourl4assign ,
		'description' => $body4assign ,
		'submitter' =>  $submitter ,
		'updated' => formatTimestamp(intval( $date ),'s') ,
		'date' => intval( $date ) ,
		'shots' => $shots_link ,
		'broken_link' => $broken_link ,
	) ;
}

// display stage
xoops_cp_header();
include dirname(__FILE__).'/mymenu.php' ;
require_once XOOPS_ROOT_PATH.'/class/template.php' ;
$tpl =& new XoopsTpl() ;
$tpl->assign( array(
	'mod_url' => $mod_url ,
	'page' => 'approvalmanager' ,
	'history' => $history4assign ,
	'historylist' => $historylist ,
	'down' => $download4assign ,
) ) ;
$tpl->display( 'db:'.$mydirname.'_admin_history.html' ) ;
xoops_cp_footer();

?>